package com.oak.springbootredis;

import com.oak.springbootredis.config.DistributeLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * com.oak.springbootredis -> TestService
 *
 * @Author: guoxy
 * @Description:
 * @Date: 2022/4/26
 */
@Service
public class TestServiceImpl implements TestService{

    /**
     * @@缓存注解@@第三步@@使用注解存储结果集到redis缓存
     * 此处@Cacheable（获取返回值，没有则执行方法，并添加到缓存）
     * 另外还有@CacheEvict（清除缓存）、@CachePut（新增/更新缓存）注解可用
     */
    @Cacheable(cacheNames="MY_FRIEND:addFriends",key = "#nickname")
    @Override
    public User addFriends(String nickname,String name){
        return new User().setUserId(UUID.randomUUID().toString())
                .setNickname(nickname)
                .setName(name);
    }

    /**
     *
     */
    @DistributeLock(prefix = "USER_INFO:",value = "#user.idCard")
    @Override
    public void addFriendInfo(User user){
        // 数据入库
    }

}